//
//  LYIntegrationViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/4.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYIntegrationViewController.h"
#import "LYIntegrationHeaderView.h"
#import "LYLYIntegrationCell.h"
#import "LYMyIntegralListModel.h"

@interface LYIntegrationViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSArray *models;

@property (weak, nonatomic) LYIntegrationHeaderView *header;

@end

@implementation LYIntegrationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view from its nib.
    [self getMineIntegration];
    [self setUpTableView];

}

#pragma mark - 网络请求
/**
 *  签到
 */
- (void)sign{

    //签到
    [LYRequestTool userSiginWithToken:LYToken success:^{
        
        [MBProgressHUD showSuccess:@"签到成功"];
        [self getMineIntegration];
        
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD showOnlyText:message];
    }];


}

/**
 *  积分日志
 */
- (void)getIntegrationList{

    [LYRequestTool getIntegralListWithToken:LYToken success:^(NSArray<LYMyIntegralListModel *> *models) {
        
        self.models = models;
        [self.tableView reloadData];
        
    } failure:^(NSString *code, NSString *message) {
        
        
        
    }];

}

/**
 *  获取积分
 */
- (void)getMineIntegration{

    [LYRequestTool getMyIntegralWithToken:LYToken success:^(NSString *integral) {
        
        self.header.integrationValue = [integral integerValue];
        [self getIntegrationList];
        
    } failure:^(NSString *code, NSString *message) {
        
    }];
}



#pragma mark - 初始化

- (void)setUpTableView{

    
    LYIntegrationHeaderView *header = [LYIntegrationHeaderView viewForNib];
    __weak typeof(self) weakSelf = self;
    header.block = ^{
        
        [weakSelf sign];
        
    };
    
    header.frame = CGRectMake(0, 0, LYScreenWidth, 92);
    self.tableView.tableHeaderView = header;
    self.header = header;
}


#pragma mark - tableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 43;

}

#pragma mark - tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"LYLYIntegrationCell";
    LYLYIntegrationCell *cell = [LYLYIntegrationCell cellWithTableView:tableView andIndetifier:cellID];
    
    //model
    LYMyIntegralListModel *model = self.models[indexPath.row];
    cell.model = model;
    
    return cell;
}



@end
